'use strict';
const {getDate} = require('./date');
/**
 * [date 日期时间处理函数]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   format [description]
 * @param    {[type]}   time   [description]
 * @return   {[type]}          [description]
 */
exports.date = function(format,time){
	try{
		return getDate(format,time);
	} catch(err){
		console.error(err.message);
		return false;
	}
}
/**
 * [time 获取当前时间戳]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @return   {[type]}   [description]
 */
exports.time = function(timer = false){
    if(timer === true){
        return new Date().getTime();
    } else {
        return Date.parse(new Date())/1000;
    }
}

/**
 * 将日期型转时间戳
 * @作者     como
 * @时间     2018-05-02
 * @版权     思智捷信息管理系统
 * @版本     1.0.0
 * @param  {String}   date [description]
 * @return {[type]}        [description]
 */
exports.strToTime = function(formatDate = getDate('Y-m-d')){
    try{
        let curdate = new Date(formatDate);
        let time = Date.parse(curdate);
        return time/1000;
    }catch(e){
    	console.error(e.message);
        return false;
    }
}
/**
 * [monthOfDay 获取某月有多少天]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   time [description]
 * @return   {[type]}        [description]
 */
exports.monthDay = function(formatDate = getDate('Y-m-d')){
	try{
        var date = new Date(formatDate)
        var year = date.getFullYear()
        var mouth = date.getMonth() + 1
        var days = 0;
        //当月份为二月时，根据闰年还是非闰年判断天数
        if (mouth == 2) {
            days = (year%4==0 && year%100==0 && year%400==0) || (year%4==0 && year%100!=0) ? 28 : 29
        } else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
            //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
            days = 31
        } else {
            //其他月份，天数为：30.
            days = 30
        }
        return days
	} catch(err){
		console.error(err.message);
		return 0;
	}
}
/**
 * [yearDay 判断某年有多少天]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {[type]}              [description]
 */
exports.yearDay = function(formatDate = getDate('Y-m-d')){
	try{
        var firstDayYear = this.yearFirstDay(formatDate);
        var lastDayYear = this.yearLastDay(formatDate);
        var numSecond = (new Date(lastDayYear).getTime() - new Date(firstDayYear).getTime())/1000;
        return Math.ceil(numSecond/(24*3600));
	} catch(err){
		console.error(err.message);
		return 0;
	}
}
/**
 * [yearFirstDay 获取一年的第一天]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {[type]}              [description]
 */
exports.yearFirstDay = function(formatDate = getDate('Y-m-d')){
	try{
        var year = new Date(formatDate).getFullYear();
        return year + "-01-01 00:00:00";
	} catch(err){
		console.error(err.message);
		return false;
	}
}
/**
 * [yearLastDay 获取一年的最后一天]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {[type]}              [description]
 */
exports.yearLastDay = function(formatDate = getDate('Y-m-d')){
	try{
        var year = new Date(formatDate).getFullYear();
        var dateString = year + "-12-01 00:00:00";
        var endDay = this.monthDay(dateString);
        return year + "-12-" + endDay + " 23:59:59";
	} catch(err){
		console.error(err.message);
		return false;
	}
}
/**
 * [dateToDay 获取某个日期是当年中的第几天]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {[type]}              [description]
 */
exports.dateToYearDay = function(formatDate = getDate('Y-m-d')){
	try{
        var firstDayYear = this.yearFirstDay(formatDate);
        var numSecond = (new Date(formatDate).getTime() - new Date(firstDayYear).getTime())/1000;
        return Math.ceil(numSecond/(24*3600));
	} catch(err){
		console.error(err.message);
		return false;
	}
}
/**
 * [dateToYearWeek 获取某个日期在这一年的第几周]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {[type]}              [description]
 */
exports.dateToYearWeek = function(formatDate = getDate('Y-m-d')){
	try{
        var numdays = this.dateToYearDay(formatDate);
        return Math.ceil(numdays / 7);
	} catch(err){
		console.error(err.message);
		return false;
	}
}
/**
 * [isLeapYear 判断是否润年]
 * @Author   szjcomo
 * @DateTime 2019-09-20
 * @param    {[type]}   formatDate [description]
 * @return   {Boolean}             [description]
 */
exports.isLeapYear = function(formatDate = getDate('Y-m-d')){
	try{
		var year = new Date(formatDate).getFullYear();
    	return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;		
	} catch(err){
		console.error(err.message);
		return false;
	}
}

